package com.serviceprovider.business.module.admin.acl.utility;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.acls.Acl;
import org.springframework.security.acls.AclService;
import org.springframework.security.acls.NotFoundException;
import org.springframework.security.acls.jdbc.LookupStrategy;
import org.springframework.security.acls.objectidentity.ObjectIdentity;
import org.springframework.security.acls.objectidentity.ObjectIdentityImpl;
import org.springframework.security.acls.sid.Sid;
import org.springframework.util.Assert;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

/**
 * Simple JDBC-based implementation of <code>AclService</code>.<p>Requires the "dirty" flags in {@link
 * org.springframework.security.acls.domain.AclImpl} and {@link org.springframework.security.acls.domain.AccessControlEntryImpl} to be set,
 * so that the implementation can detect changed parameters easily.</p>
 */
public class JdbcAclService implements AclService {
    protected static final Log  log                       = LogFactory.getLog(JdbcAclService.class);
    private static final String selectAclObjectWithParent =
        "SELECT obj.object_id_identity AS obj_id, " + "       class.class AS class "
        + "  FROM acl_object_identity obj, " + "       acl_object_identity parent, " + "       acl_class class "
        + " WHERE obj.parent_object = parent.id " + "   AND obj.object_id_class = class.id "
        + "   AND parent.object_id_identity = ? "
        + "   AND parent.object_id_class = (SELECT id FROM acl_class WHERE acl_class.class = ?)";
    protected JdbcTemplate jdbcTemplate;
    private LookupStrategy lookupStrategy;

    public JdbcAclService(DataSource dataSource, LookupStrategy lookupStrategy) {
        Assert.notNull(dataSource, "DataSource required");
        Assert.notNull(lookupStrategy, "LookupStrategy required");
        this.jdbcTemplate   = new JdbcTemplate(dataSource);
        this.lookupStrategy = lookupStrategy;
    }

    public ObjectIdentity[] findChildren(ObjectIdentity parentIdentity) {
        Object[] args    = { parentIdentity.getIdentifier(), parentIdentity.getJavaType().getName() };
        List     objects = jdbcTemplate.query(selectAclObjectWithParent, args, new RowMapper() {
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                String javaType   = rs.getString("class");
                String identifier = rs.getString("obj_id");

                return new ObjectIdentityImpl(javaType, identifier);
            }
        });

        return (ObjectIdentityImpl[]) objects.toArray(new ObjectIdentityImpl[] {});
    }

    public Acl readAclById(ObjectIdentity object, Sid[] sids) throws NotFoundException {
        Map map = readAclsById(new ObjectIdentity[] { object }, sids);

        if (map.size() == 0) {
            throw new NotFoundException("Could not find ACL");
        } else {
            return (Acl) map.get(object);
        }
    }

    public Acl readAclById(ObjectIdentity object) throws NotFoundException {
        return readAclById(object, null);
    }

    public Map readAclsById(ObjectIdentity[] objects) {
        return readAclsById(objects, null);
    }

    public Map readAclsById(ObjectIdentity[] objects, Sid[] sids) throws NotFoundException {
        return lookupStrategy.readAclsById(objects, sids);
    }
}



